package org.supda.system.controller.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.supda.system.annoation.SysLog;
import org.supda.system.controller.common.BaseController;
import org.supda.system.entity.TResources;
import org.supda.system.entity.TRole;
import org.supda.system.service.ResourceService;
import org.supda.system.service.RoleService;
import org.supda.system.util.DataTablePageUtil;
/**
 * 
 * @author supda
 * @Date 2016-12-29 下午2:22:16
 * 角色管理
 */
@Controller
@RequestMapping("/role")
public class RoleController  extends BaseController{
	
	@Inject private RoleService roleService;
	@Inject private ResourceService resourceService;
	
	/**
	 * 跳转到角色管理页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value ={"/",""}, method=RequestMethod.GET, produces="text/html;charset=utf-8")
	public String roleUI(HttpServletRequest request, Model model){
		//查询出子菜单的所有操作
		model.addAttribute("menuId",getOID());
		return SYSTEM_MANAGEMENT_VIEW_PACKAGE+"role/role";
	}
	
	/**
	 * 角色数据列表
	 * @param request
	 * @return
	 */
	 @RequestMapping(value = "/search",method=RequestMethod.POST, produces="text/html;charset=utf-8")  
	    public @ResponseBody DataTablePageUtil<TRole> search(WebRequest request){  

    	DataTablePageUtil<TRole> dtp = new DataTablePageUtil<TRole>(request); 
    	
    	//开启Or查询
    	roleService.listByDataTable(dtp);
    	
    	return dtp; 
	}
	 
	 /**
	  * 加载角色编辑页面
	  * @param request
	  * @param roleId
	  * @param model
	  * @return
	  */
	 @RequestMapping(value = "/edit",method=RequestMethod.GET, produces="text/html;charset=utf-8")  
	 public String editUI(
			 HttpServletRequest request ,
			 @RequestParam(value="id") Long id,
			 Model model){
		
		//取出所有的资源
		List<TResources> listResources = resourceService.listResources();
		
		//取出该角色所拥有的资源
		List<TResources> listBelongRes = roleService.selectRoleCorrelationResources(id);
		
		model.addAttribute("role", roleService.findByRoleId(id));
		
		model.addAttribute("listResources" , listResources);
		model.addAttribute("listBelongRes" , listBelongRes);
		
		return SYSTEM_MANAGEMENT_VIEW_PACKAGE+"role/role-edit";
	 }
	
	 
	 /**
	  * 角色编辑
	  * @param request
	  * @param roleId
	  * @param model
	  * @return
	  */
		@SysLog(module="角色编辑", method="edit", desc="角色编辑")
	 @RequestMapping(value = "/edit",method=RequestMethod.POST, produces="text/html;charset=utf-8")  
	public @ResponseBody String edit(
			 HttpServletRequest request ,
			 @RequestParam(value="id") Long id,
			 Long[] resId){
		
		if(id != null && resId != null && resId.length >0){
			
			//取出该角色所拥有的资源
			List<TResources> listBelongRes = roleService.selectRoleCorrelationResources(id);
			
			//需解除关系的权限
			List<Long> unResIds = new ArrayList<Long>(); 
			
			//新增的权限
			List<Long> newResIds = new ArrayList<Long>(); 
			
			warp(listBelongRes , unResIds , newResIds , resId);
			
			//更新角色权限
			roleService.updateRelationResources(id, unResIds, newResIds);
		}
		
		return RESPONSE_SUCCESS;
	}
	 
	 /**
	  * 1.进入添加角色页面 GET:/role_create
	  */
	 @RequestMapping(value = "/create",method=RequestMethod.GET, produces="text/html;charset=utf-8")  
	 public String createUI(
			 HttpServletRequest request ,
			 Model model){
		
			//取出所有的资源
			List<TResources> listResources = resourceService.listResources();
			
			model.addAttribute("listResources" , listResources);
			
			return SYSTEM_MANAGEMENT_VIEW_PACKAGE+"role/role-create";
	 }
	 
	 /**
	  * 2.添加角色 POST:/role_create
	  */
	 @RequestMapping(value = "/create",method=RequestMethod.POST, produces="text/html;charset=utf-8")  
	 public @ResponseBody String create(
			 HttpServletRequest request ,
			 TRole role,
			 Long[] resIds,
			 Model model){
		
		 TRole nr = roleService.createRole(role , resIds);
		 
		 if(nr != null)
			 return RESPONSE_SUCCESS;
		 else
			 return RESPONSE_FAIL;
	 }
	 
	 /**
	  * 3.删除角色 DELETE:/role_delete?id={id}
	  */
	 @RequestMapping(value = "/delete",method=RequestMethod.POST, produces="text/html;charset=utf-8")  
	 public @ResponseBody String delete(
			 HttpServletRequest request ,
			 @RequestParam(value="id") Long id,
			 Model model){
		
		 try{
		 roleService.deleteRoleLogic(id);
		 }catch(Exception e){
			 return RESPONSE_FAIL;
		 }
		return RESPONSE_SUCCESS;
	 }
	 
	 /**
	  * 6.锁定角色 PUT:/role_locked?id={id}
	  * 
	  */
	 @RequestMapping(value = "/locked",method=RequestMethod.POST, produces="text/html;charset=utf-8")  
	 public @ResponseBody String locked(
			 HttpServletRequest request ,
			 @RequestParam(value="id") Long id,
			 Model model){
		
		 String message = ""; 
		 
		message =  roleService.locked(id);
			 
		return message;
	 }
	 
	//封装关系数据
	private void warp(List<TResources> listBelongRes, List<Long> unResIds,
			List<Long> newResIds , Long[] resIdes) {
		
		List<Long> belongRes = new ArrayList<Long>();
		List<Long> listResIdes = Arrays.asList(resIdes);
		
		for(TResources tr : listBelongRes) {
			belongRes.add(tr.getId());
		}
		
		for(Long resId : resIdes){
			//不存在该权限
			if(!belongRes.contains(resId)){
				newResIds.add(resId);
			}
		}
		
		for(Long bRs : belongRes){
			//提交的数据中没有原权限id
			if(!listResIdes.contains(bRs)){
				unResIds.add(bRs);
			}
		}
	}
}
